//
//  TongZhiDetailViewController.m
//  Cunli
//
//  Created by 湛亚 on 15/6/23.
//  Copyright (c) 2015年 lilubin. All rights reserved.
//

#import "TongZhiDetailViewController.h"
#import "TongZhiConnect.h"
#import "ContentText.h"
//#import "PlaceholderTextView.h"
#import "MyDataService.h"
#import "NSString+MD5.h"
#import "UIImageView+WebCache.h"
#import "PeopleDataViewController.h"
#import "MJRefresh.h"
#import "YMButton.h"
#import "UMSocial.h"
#import "UMSocialWechatHandler.h"
#import "UMSocialQQHandler.h"
#import "HYActivityView.h"
#import "ReportDetailVC.h"
#import "PointsManager.h"
#import "ConvertToCommonEmoticonsHelper.h"
#import "DXMessageToolBar.h"
#import "TTTAttributedLabel.h"
#import "StudyCommentTableViewCell.h"
#import "WFPopThreeView.h"
#import "UIColor+Addition.h"

@interface TongZhiDetailViewController ()<UITextViewDelegate,UIWebViewDelegate,DXMessageToolBarDelegate,TTTAttributedLabelDelegate>{
    
    TongZhiConnect *tongZhi;
    float screenWidth,screenHeight,webHeight;
    int contentPage;
    NSMutableArray *contentArray;
    UILabel *tiShiLabel;
    UIButton *imageBtn;
    //PlaceholderTextView *contentText;
    //UIView *bgView;
    UIWebView *contentWebView;
    //int keyPosition,keyHeight;
    UIButton *rightBtn;
    NSMutableDictionary *zanDic;
    BOOL isAddRefresh;
}
@property (nonatomic, strong) HYActivityView *activityView;
@property (strong, nonatomic) DXMessageToolBar *chatToolBar;
@property (nonatomic,strong) WFPopThreeView *operationView;
@property (nonatomic,strong) NSIndexPath *selectedIndexPath;
@end

@implementation TongZhiDetailViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    isAddRefresh=NO;//是否添加下拉刷新
    contentPage=1;//初始化页数
    webHeight=50;
    screenWidth=[UIScreen mainScreen].bounds.size.width;
    screenHeight=[UIScreen mainScreen].bounds.size.height;
    
    contentArray=[[NSMutableArray alloc]init];//评论数组
    zanDic=[[NSMutableDictionary alloc]init];//存储已赞的评论id
    self.view.backgroundColor=[UIColor convertHexColorToUIColor:0xf6f6f6];
    if (self.type==1) {
        self.title=@"通知详情";
    }else{
        self.title=@"通告详情";
    }
    
    [self getTongZhiInfo];//获取通知详情
    [self createTableView];
    [self createNavButton];
    //[self addRefreshAttribute];
    
    if (self.type==1) {
        //通知界面添加评论框
        _tongZhiTableView.frame=CGRectMake(0, 0, screenWidth, screenHeight-104);
        [self.view addSubview:self.chatToolBar];
    }
}

-(void)createNavButton{
    
    UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    leftBtn.frame = CGRectMake(0, 0, 28, 28);
    [leftBtn setImage:[UIImage imageNamed:@"regback01"] forState:UIControlStateNormal];
    leftBtn.contentEdgeInsets = UIEdgeInsetsMake(0, -20, 0, 0);
    [leftBtn addTarget:self action:@selector(goBack) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftBarItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
    self.navigationItem.leftBarButtonItem = leftBarItem;
    rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    rightBtn.frame = CGRectMake(0, 0, 25, 20);
    [rightBtn setImage:[UIImage imageNamed:@"nav_right_share"] forState:UIControlStateNormal];
    [rightBtn addTarget:self action:@selector(more) forControlEvents:UIControlEventTouchUpInside];
    rightBtn.userInteractionEnabled=NO;
    UIBarButtonItem *rightBarItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
    self.navigationItem.rightBarButtonItem = rightBarItem;
}

-(void)createTableView{
    
    _tongZhiTableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, screenWidth, screenHeight-68) style:UITableViewStyleGrouped];
    _tongZhiTableView.dataSource=self;
    _tongZhiTableView.delegate=self;
    _tongZhiTableView.backgroundColor=[UIColor convertHexColorToUIColor:0xf6f6f6];
    _tongZhiTableView.separatorStyle=NO;
    [self.view addSubview:_tongZhiTableView];
}

-(void)keyBoardHidden
{
    [self.chatToolBar endEditing:YES];
}

- (DXMessageToolBar *)chatToolBar
{
    if (_chatToolBar == nil) {
        
        _chatToolBar = [[DXMessageToolBar alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - [DXMessageToolBar defaultHeight], screenWidth, [DXMessageToolBar defaultHeight])];
        _chatToolBar.isComment=YES;
        _chatToolBar.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleRightMargin;
        _chatToolBar.delegate = self;
    }
    
    return _chatToolBar;
}

-(void)getTongZhiInfo{
    
    NSMutableDictionary *CheckParams =[[NSMutableDictionary alloc]init];
    [CheckParams setObject:[Config getOwnUserID] forKey:@"userid"];
    [CheckParams setObject:[NSString stringWithFormat:@"%d",self.itemid] forKey:@"itemid"];
    NSString *resultSign = [NSString getSignFromParam:CheckParams andSession:[Config getOwnSession]];
    [CheckParams setObject:resultSign forKey:@"sign"];
    [MyDataService requestURL:@"convenientdetail.php" httpMethod:@"GET" params:CheckParams fileDatas:nil complection:^(id result) {
        
        NSLog(@"========%@",result);
        
        if ([[result objectForKey:@"response"] isEqualToString:@"convenientdetail"]) {
            //tongZhi=[[TongZhiConnect alloc]initWithDictionary:[result objectForKey:@"convenientinfo"]];
            tongZhi=[TongZhiConnect yy_modelWithDictionary:[result objectForKey:@"convenientinfo"]];
            contentWebView=[[UIWebView alloc]initWithFrame:CGRectMake(0, 5, screenWidth, 20)];
            contentWebView.delegate=self;
            contentWebView.userInteractionEnabled=NO;
            NSMutableString* contentString=[NSMutableString stringWithFormat:@"%@",tongZhi.content];
            //[contentString insertString:@"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" atIndex:30];
            //NSLog(@">>>>>>>>>%@",contentString);
            [contentWebView loadHTMLString:contentString baseURL:nil];
            
            [self.tongZhiTableView reloadData];
            rightBtn.userInteractionEnabled=YES;
            if (self.type==1) {
                
                [self getPingLunInfoWithPage:1];
            }
            
        }else{
            MBProgressHUD *hud = [Config createHUD];
            hud.mode = MBProgressHUDModeCustomView;
            hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"error"]];
            hud.labelText = @"获取详情失败!";
            [hud hide:YES afterDelay:1];
        }
        
    }failureAction:^{
        
    }];
}

-(void)webViewDidFinishLoad:(UIWebView *)webView{
    
    CGFloat webViewHeight=[webView.scrollView contentSize].height;
    CGRect newFrame = webView.frame;
    newFrame.size.height = webViewHeight;
    webView.frame = newFrame;
    [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.background='#f6f6f6'"];
    [self.tongZhiTableView reloadData];
}

-(void)more{
    
    if ([Config isShield]) {
        [self showAlertWithMessage:@"您已被村联络员屏蔽在该村的发言。如需申诉,请到“我-消息-通讯录”中与联络员进行联系"];
        return;
    }
    
    [self.view endEditing:YES];
    __weak typeof(self) weakSelf = self;
    
    if (!self.activityView) {
        self.activityView = [[HYActivityView alloc]initWithTitle:@"分享到" referView:self.view];
        
        //横屏会变成一行6个, 竖屏无法一行同时显示6个, 会自动使用默认一行4个的设置.
        self.activityView.numberOfButtonPerLine = 6;
        
        ButtonView *bv = [[ButtonView alloc]initWithText:@"QQ" image:[UIImage imageNamed:@"cunli_qqFriend"] handler:^(ButtonView *buttonView){
            NSLog(@"点击QQ");
            [UMSocialData defaultData].extConfig.qqData.qqMessageType = UMSocialQQMessageTypeDefault;
            [UMSocialData defaultData].extConfig.qqData.title = tongZhi.title;
            
            [UMSocialData defaultData].extConfig.qqData.url = tongZhi.share_url;
            
            [[UMSocialDataService defaultDataService] postSNSWithTypes:@[UMShareToQQ] content:tongZhi.share_url image:nil location:nil urlResource:nil presentedController:self completion:^(UMSocialResponseEntity *response) {
                if (response.responseCode == UMSResponseCodeSuccess) {
                    NSLog(@"QQ分享成功");
                    
                    [PointsManager sendSharePointWithChannel:@"4" andItemid:[NSString stringWithFormat:@"%d",tongZhi.itemid]];
                }
            }];
            
        }];
        [self.activityView addButtonView:bv];
        
        bv = [[ButtonView alloc]initWithText:@"QQ空间" image:[UIImage imageNamed:@"cunli_qqZone"] handler:^(ButtonView *buttonView){
            //            NSLog(@"点击QQ空间");
            
            [UMSocialData defaultData].extConfig.qqData.qqMessageType = UMSocialQQMessageTypeDefault;
            [UMSocialData defaultData].extConfig.qzoneData.title = tongZhi.title;
            [UMSocialData defaultData].extConfig.qzoneData.url = tongZhi.share_url;
            
            [[UMSocialDataService defaultDataService] postSNSWithTypes:@[UMShareToQzone] content:tongZhi.title image:[UIImage imageNamed:@"aboutApp01.png"] location:nil urlResource:nil presentedController:self completion:^(UMSocialResponseEntity *response) {
                if (response.responseCode == UMSResponseCodeSuccess) {
                    //                    NSLog(@"QQ空间分享成功");
                    
                    [PointsManager sendSharePointWithChannel:@"3" andItemid:[NSString stringWithFormat:@"%d",tongZhi.itemid]];
                }
            }];
            
        }];
        [self.activityView addButtonView:bv];
        
        bv = [[ButtonView alloc]initWithText:@"微信" image:[UIImage imageNamed:@"cunli_wechatFriend"] handler:^(ButtonView *buttonView){
            
            //            NSLog(@"点击微信");
            [UMSocialData defaultData].extConfig.wxMessageType = UMSocialWXMessageTypeWeb;
            [UMSocialData defaultData].extConfig.wechatSessionData.url = tongZhi.share_url;
            [UMSocialData defaultData].extConfig.title = tongZhi.title;
            
            [[UMSocialDataService defaultDataService] postSNSWithTypes:@[UMShareToWechatSession] content:tongZhi.title image:nil location:nil urlResource:nil presentedController:self completion:^(UMSocialResponseEntity *response) {
                //                NSLog(@"分享成功");
                
                [PointsManager sendSharePointWithChannel:@"1" andItemid:[NSString stringWithFormat:@"%d",tongZhi.itemid]];
            }];
        }];
        [self.activityView addButtonView:bv];
        
        bv = [[ButtonView alloc]initWithText:@"朋友圈" image:[UIImage imageNamed:@"cunli_wechatTimeline"] handler:^(ButtonView *buttonView){
            //            NSLog(@"朋友圈");
            [UMSocialData defaultData].extConfig.wxMessageType = UMSocialWXMessageTypeWeb;
            [UMSocialData defaultData].extConfig.wechatTimelineData.url = tongZhi.share_url;
            [UMSocialData defaultData].extConfig.title = tongZhi.title;
            
            [[UMSocialDataService defaultDataService] postSNSWithTypes:@[UMShareToWechatTimeline] content:tongZhi.title image:nil location:nil urlResource:nil presentedController:self completion:^(UMSocialResponseEntity *response) {
                //                NSLog(@"分享成功");
                
                [PointsManager sendSharePointWithChannel:@"2" andItemid:[NSString stringWithFormat:@"%d",tongZhi.itemid]];
            }];
            
        }];
        [self.activityView addButtonView:bv];
        
        bv = [[ButtonView alloc]initWithText:@"收藏" image:[UIImage imageNamed:@"cunli_favorite"] handler:^(ButtonView *buttonView){
            [weakSelf doFavoriteAction];
        }];
        [self.activityView addButtonView:bv];
        
        bv = [[ButtonView alloc]initWithText:@"举报" image:[UIImage imageNamed:@"cunli_report"] handler:^(ButtonView *buttonView){
            [weakSelf doReportAction];
        }];
        [self.activityView addButtonView:bv];
        
    }
    
    [self.activityView show];
}

- (void)doReportAction
{
    
    ReportDetailVC *reportVC = [[ReportDetailVC alloc] init];
    reportVC.itemid = [NSString stringWithFormat:@"%d",tongZhi.itemid];
    reportVC.m_itemid = [NSString stringWithFormat:@"%d",tongZhi.item_mid];
    [self.navigationController pushViewController:reportVC animated:YES];
}

//加入收藏
-(void)doFavoriteAction{
    
    NSMutableDictionary *CheckParams =[[NSMutableDictionary alloc]init];
    [CheckParams setObject:[Config getOwnUserID] forKey:@"userid"];
    [CheckParams setObject:[NSString stringWithFormat:@"%d",tongZhi.item_mid] forKey:@"item_mid"];
    [CheckParams setObject:[NSString stringWithFormat:@"%d",tongZhi.itemid] forKey:@"itemid"];
    [CheckParams setObject:@"1" forKey:@"type"];
    NSString *resultSign = [NSString getSignFromParam:CheckParams andSession:[Config getOwnSession]];
    [CheckParams setObject:resultSign forKey:@"sign"];
    [MyDataService requestURL:@"add_favorite.php" httpMethod:@"GET" params:CheckParams fileDatas:nil complection:^(id result) {
        
        NSLog(@"========%@",result);
        if ([[result objectForKey:@"response"] isEqualToString:@"add_favorite"]) {
            MBProgressHUD *hud = [Config createHUD];
            hud.mode = MBProgressHUDModeCustomView;
            hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"done"]];
            hud.labelText = @"收藏成功!";
            [hud hide:YES afterDelay:1];
        }else{
            
            NSString *errorStr=[[result objectForKey:@"error"] objectForKey:@"text"];
            MBProgressHUD *hud = [Config createHUD];
            hud.mode = MBProgressHUDModeCustomView;
            hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"error"]];
            hud.labelText = errorStr;
            [hud hide:YES afterDelay:1];
        }
    }failureAction:^{
        
    }];
}

//将要开始编辑
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView{
    
    return YES;
}

- (void)didSendText:(NSString *)text
{
    if ([text isEqualToString:@""]) {
        [self showAlertWithMessage:@"输入内容不能为空"];
        return;
    }else{
        
        NSString *willSendText = [ConvertToCommonEmoticonsHelper convertToCommonEmoticons:text];
        NSLog(@">>>>>>>%@",willSendText);
        [self sendContentWithString:willSendText];
    }
}

-(void)sendContentWithString:(NSString *)comment{
    
    NSMutableDictionary *CheckParams =[[NSMutableDictionary alloc]init];
    [CheckParams setObject:[Config getOwnUserID] forKey:@"userid"];
    [CheckParams setObject:[NSString stringWithFormat:@"%d",tongZhi.itemid] forKey:@"itemid"];
    [CheckParams setObject:[NSString stringWithFormat:@"%d",tongZhi.item_mid] forKey:@"item_mid"];
    [CheckParams setObject:comment forKey:@"content"];
    
    NSString *resultSign = [NSString getSignFromParam:CheckParams andSession:[Config getOwnSession]];
    [CheckParams setObject:resultSign forKey:@"sign"];
    [MyDataService requestURL:@"comment.php" httpMethod:@"POST" params:CheckParams fileDatas:nil complection:^(id result) {
        
        NSLog(@"%@",result);
        if ([[result objectForKey:@"response"] isEqualToString:@"comment"]) {
            
            MBProgressHUD *hud = [Config createHUD];
            hud.mode = MBProgressHUDModeCustomView;
            hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"done"]];
            hud.labelText = @"发送成功!";
            [hud hide:YES afterDelay:1];
        }else{
            NSString *str=[[result objectForKey:@"error"] objectForKey:@"text"];
            if (str) {
                [self showAlertWithMessage:str];
            }else{
                [self showAlertWithMessage:@"网络错误,发送失败!"];
            }
            return ;
        }
        
        [self.chatToolBar endEditing:YES];
        [self getPingLunInfoWithPage:1];
        
    }failureAction:^{
        
    }];
    
}

-(void)goBack{
    
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)getPingLunInfoWithPage:(int)page{
    
    NSMutableDictionary *CheckParams =[[NSMutableDictionary alloc]init];
    [CheckParams setObject:[Config getOwnUserID] forKey:@"userid"];
    NSString *item_mid=[NSString stringWithFormat:@"%d",tongZhi.item_mid];
    [CheckParams setObject:item_mid forKey:@"item_mid"];
    NSString *itemid=[NSString stringWithFormat:@"%d",tongZhi.itemid];
    [CheckParams setObject:itemid forKey:@"itemid"];
    [CheckParams setObject:@"1" forKey:@"page"];
    [CheckParams setObject:@"10000" forKey:@"pagesize"];
    NSString *resultSign = [NSString getSignFromParam:CheckParams andSession:[Config getOwnSession]];
    [CheckParams setObject:resultSign forKey:@"sign"];
    
    NSString *url=@"commentlist.php";
    //NSString *url=@"hot_commentlist.php";
    [MyDataService requestURL:url httpMethod:@"GET" params:CheckParams fileDatas:nil complection:^(id result) {
        
        NSLog(@"========%@",result);
        
        if ([[result objectForKey:@"response"] isEqualToString:@"commentlist"]) {
            NSArray *commentlistArray=[result objectForKey:@"commentlist"];
            
            if (commentlistArray.count==0) {
                [contentArray removeAllObjects];
                [self.tongZhiTableView reloadData];
                return ;
            }
            
            [contentArray removeAllObjects];
            for (NSDictionary *dic in commentlistArray) {
                //ContentText *content=[[ContentText alloc]initWithDictionary:dic];
                ContentText *content=[ContentText yy_modelWithDictionary:dic];
                [contentArray addObject:content];
            }
            
            [self.tongZhiTableView reloadData];
        }else{
            
            MBProgressHUD *hud = [Config createHUD];
            hud.mode = MBProgressHUDModeCustomView;
            hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"error"]];
            hud.labelText = @"获取评论失败!";
            [hud hide:YES afterDelay:1];
        }
        
    }failureAction:^{
        
    }];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (!tongZhi.title) {
        return 0;
    }
    
    if (section==0) {
        if (self.type==1) {
            return 6;
        }
        return 5;
    }else{
        if (contentArray.count<1) {
            return 1;
        }else{
            return contentArray.count;
        }
    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 1;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    if (self.type==1) {
        return 2;
    }
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *cellIdentifier=@"cell";
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if(!cell){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    cell.backgroundColor=[UIColor convertHexColorToUIColor:0xf6f6f6];
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    if (indexPath.section==0) {
        
        switch (indexPath.row){
            case 0:{
                
                UILabel *titleLab=[[UILabel alloc]initWithFrame:CGRectMake(10, 5, screenWidth-20, 50)];
                titleLab.textAlignment=NSTextAlignmentCenter;
                titleLab.numberOfLines=0;
                titleLab.font=[UIFont systemFontOfSize:20];
                titleLab.text=tongZhi.title;
                [cell addSubview:titleLab];
                return cell;
            }
                break;
            case 1:{
                UILabel *objectLab=[[UILabel alloc]initWithFrame:CGRectMake(10, 5, screenWidth-20, 20)];
                objectLab.text=tongZhi.object;
                objectLab.textAlignment=1;
                objectLab.textColor=[UIColor colorWithRed:18/255.0 green:133/255.0 blue:210/255.0 alpha:1];
                [cell addSubview:objectLab];
                return cell;
            }
                break;
            case 2:{
                
                [cell addSubview:contentWebView];
                return cell;
            }
                break;
            case 3:{
                
                UILabel *publishLab=[[UILabel alloc]initWithFrame:CGRectMake(10, 5, screenWidth-20, 20)];
                publishLab.text=tongZhi.publish;
                publishLab.textAlignment=NSTextAlignmentRight;
                [cell addSubview:publishLab];
                return cell;
            }
                break;
            case 4:{
                
                UILabel *addtimeLab=[[UILabel alloc]initWithFrame:CGRectMake(10, 5, screenWidth-20, 20)];
                addtimeLab.textAlignment=NSTextAlignmentRight;
                addtimeLab.text=tongZhi.addtime;
                [cell addSubview:addtimeLab];
                return cell;
            }
                break;
            case 5:{
                
                UIView *bgView=[[UIView alloc]initWithFrame:CGRectMake(10, 22, screenWidth-20, 1)];
                bgView.backgroundColor=[UIColor convertHexColorToUIColor:0xdcdddd];
                [cell addSubview:bgView];
                UILabel *titleLab=[[UILabel alloc]initWithFrame:CGRectMake((screenWidth-90)/2.0, 7, 90, 30)];
                titleLab.text=@"热门讨论";
                titleLab.textColor=[UIColor convertHexColorToUIColor:0x333333];
                titleLab.backgroundColor=[UIColor convertHexColorToUIColor:0xf6f6f6];
                titleLab.textAlignment=1;
                [cell addSubview:titleLab];
                return cell;
            }
                break;
            default:
                return cell;
                
        }
        
    }else{
        
        if (contentArray.count<1) {
            
            tiShiLabel=[[UILabel alloc]initWithFrame:CGRectMake(10, 5, 150, 20)];
            tiShiLabel.text=@"快来添加讨论吧";
            tiShiLabel.textColor=[UIColor convertHexColorToUIColor:0x333333];
            tiShiLabel.font=[UIFont systemFontOfSize:15];
            [cell addSubview:tiShiLabel];
            return cell;
            
        }else{
            
            [tiShiLabel removeFromSuperview];
            ContentText *content=[contentArray objectAtIndex:indexPath.row];
            static NSString *cellIdentifier1=@"cell1";
            
            StudyCommentTableViewCell *commentCell =(StudyCommentTableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
            if (!commentCell) {
                
                commentCell = [[StudyCommentTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier1 andContent:content];
            }
            
            //头像点击事件
            commentCell.headImg.tag=indexPath.row;
            UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(lookPersonInfo:)];
            [commentCell.headImg addGestureRecognizer:singleTap];
            //姓名点击
            commentCell.nameLabel.tag=indexPath.row;
            UITapGestureRecognizer *nameSingleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(lookPersonInfo:)];
            [commentCell.nameLabel addGestureRecognizer:nameSingleTap];
            
            //网址链接代理
            //commentCell.commentLabel.delegate=self;
            //更多操作
            commentCell.moreButton.appendIndexPath=indexPath;
            [commentCell.moreButton addTarget:self action:@selector(showMoreButton:) forControlEvents:UIControlEventTouchUpInside];

            return commentCell;
        }
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [self.operationView dismiss];
    [self.view endEditing:YES];
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (self.type==2) {
        switch (indexPath.row) {
            case 0:
                return 60;
            case 1:
            case 3:
            case 4:
                return 30;
            case 2:{
                
                return contentWebView.frame.size.height+10;
            }
            default:
                return 44;
        }
    }
    if (indexPath.section==0) {
        
        switch (indexPath.row) {
            case 0:
                return 60;
            case 1:
            case 3:
            case 4:
            case 5:
                return 30;
            case 2:{
                
                return contentWebView.frame.size.height+10;
            }
            default:
                return 44;
        }
    }else{
        
        if (contentArray.count<1) {
            return 30;
        }else{
            
            ContentText *content=[contentArray objectAtIndex:indexPath.row];
            float height=[self heightWithString:content.content];
            return height+55;
        }
        
    }
    
}

-(void)juBaoContent:(UIButton *)btn{
    
    [self.view endEditing:YES];
    ContentText *content=[contentArray objectAtIndex:btn.tag];
    ReportDetailVC *reportVC = [[ReportDetailVC alloc] init];
    reportVC.itemid = [NSString stringWithFormat:@"%d",content.itemid];
    reportVC.m_itemid =@"10";
    [self.navigationController pushViewController:reportVC animated:YES];
}

-(void)lookPersonInfo:(UIGestureRecognizer *)gestureRecognizer{
    
    ContentText *content=[contentArray objectAtIndex:gestureRecognizer.view.tag];
    PeopleDataViewController *dataVC=[[PeopleDataViewController alloc]init];
    dataVC.touserid=[NSString stringWithFormat:@"%d",content.userid];
    dataVC.hidesBottomBarWhenPushed=YES;
    [self.navigationController pushViewController:dataVC animated:YES];
}

-(void)zanContent{
    
    ContentText *content=[contentArray objectAtIndex:_selectedIndexPath.row];
    
    if ([zanDic objectForKey:[NSString stringWithFormat:@"%d",content.itemid]]) {
        
        MBProgressHUD *hud = [Config createHUD];
        hud.mode = MBProgressHUDModeCustomView;
        hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"done"]];
        hud.labelText = @"您已点过赞";
        [hud hide:YES afterDelay:1];
        
    }else{
        
        NSMutableDictionary *CheckParams =[[NSMutableDictionary alloc]init];
        [CheckParams setObject:[Config getOwnUserID] forKey:@"userid"];
        [CheckParams setObject:[NSString stringWithFormat:@"%d",content.itemid] forKey:@"itemid"];
        NSString *resultSign = [NSString getSignFromParam:CheckParams andSession:[Config getOwnSession]];
        [CheckParams setObject:resultSign forKey:@"sign"];
        [MyDataService requestURL:@"commentpraise.php" httpMethod:@"GET" params:CheckParams fileDatas:nil complection:^(id result) {
            
            NSLog(@"%@",result);
            
            if ([[result objectForKey:@"response"] isEqualToString:@"commentprise"]) {
                
                MBProgressHUD *hud = [Config createHUD];
                hud.mode = MBProgressHUDModeCustomView;
                hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"done"]];
                hud.labelText = @"点赞成功!";
                [hud hide:YES afterDelay:1];
                //存储已赞过的评论
                [zanDic setObject:@"zan" forKey:[NSString stringWithFormat:@"%d",content.itemid]];
                //改变点赞行的赞个数
                content.agree=content.agree+1;
                [contentArray replaceObjectAtIndex:_selectedIndexPath.row withObject:content];
                //NSIndexPath *indexPath=[NSIndexPath indexPathForRow:btn.tag inSection:1];
                //StudyCommentTableViewCell *commentCell =(StudyCommentTableViewCell *)[_tongZhiTableView cellForRowAtIndexPath:indexPath];
                //commentCell.zanCountLab.text=[NSString stringWithFormat:@"%d",content.agree];
                
            }else{
                
                MBProgressHUD *hud = [Config createHUD];
                hud.mode = MBProgressHUDModeCustomView;
                hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"error"]];
                hud.labelText = @"网络错误,点赞失败!";
                [hud hide:YES afterDelay:1];
            }
            
        }failureAction:^{
            
        }];
    }
    
}

-(float)heightWithString:(NSString *)introduce{
    
    CGRect rect = [introduce boundingRectWithSize:CGSizeMake(screenWidth-80, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17.0f]} context:nil];
    CGFloat height = ceilf(rect.size.height);
    return height + 10 + 10;
}

- (void)showAlertWithMessage:(NSString *)message
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:message delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [alert show];
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    
    [self.operationView dismiss];
    [self.view endEditing:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - TTTAttributedLabelDelegate 点击网址的超链接
- (void)attributedLabel:(TTTAttributedLabel *)label didSelectLinkWithURL:(NSURL *)url {
    
    NSLog(@">>>>>>%@",url);
    NSString *urlStr=[url absoluteString];
    NSRange foundObj=[urlStr rangeOfString:@"http://" options:NSCaseInsensitiveSearch];
    if(foundObj.length>0) {
        [[UIApplication sharedApplication] openURL:url];
    }else{
        
        urlStr=[NSString stringWithFormat:@"http://%@",urlStr];
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlStr]];
    }
    return;
}

#pragma mark - 按钮动画

- (void)showMoreButton:(YMButton *)sender{
    
    CGRect rectInTableView = [_tongZhiTableView rectForRowAtIndexPath:sender.appendIndexPath];
    CGFloat origin_Y = rectInTableView.origin.y + sender.frame.origin.y;
    CGRect targetRect = CGRectMake(CGRectGetMinX(sender.frame), origin_Y, CGRectGetWidth(sender.bounds), CGRectGetHeight(sender.bounds));
    if (self.operationView.shouldShowed) {
        [self.operationView dismiss];
        return;
    }
    
    _selectedIndexPath=sender.appendIndexPath;
    ContentText *content=[contentArray objectAtIndex:_selectedIndexPath.row];
    [self.operationView showAtView:_tongZhiTableView rect:targetRect isFavour:[NSString stringWithFormat:@"%d",content.agree]];
}

#pragma mark -移除展开的按钮

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    [self.operationView dismiss];
    [self.chatToolBar endEditing:YES];
}

#pragma mark -点击展开的按钮事件

- (WFPopThreeView *)operationView {
    
    if (!_operationView) {
        _operationView = [WFPopThreeView initailzerWFOperationView];
        
        __weak __typeof(self)weakSelf = self;
        
        _operationView.didSelectedOperationCompletion = ^(WFOperationType operationType) {
            switch (operationType) {
                case WFOperationTypeLike:
                    [weakSelf zanContent];
                    break;
                case WFOperationTypeReport:
                    [weakSelf reportComment];
                    break;
                case WFOperationTypeDelect:
                    [weakSelf showAlertWithTag:2002];
                    break;
                default:
                    break;
            }
        };
    }
    return _operationView;
}

#pragma mark -举报评论

- (void)reportComment{
    
    [self.view endEditing:YES];
    ContentText *content=[contentArray objectAtIndex:_selectedIndexPath.row];
    ReportDetailVC *reportVC = [[ReportDetailVC alloc] init];
    reportVC.itemid = [NSString stringWithFormat:@"%d",content.itemid];
    reportVC.m_itemid =@"10";
    [self.navigationController pushViewController:reportVC animated:YES];
}

#pragma mark -删除评论

-(void)showAlertWithTag:(NSInteger )alertTag{
    
    UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"提示" message:@"确定删除?" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    [alert show];
    alert.tag=alertTag;
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    
    if (buttonIndex==1) {
        
        if (alertView.tag==2002){
            [self delectComment];
        }
    }
}

- (void)delectComment{
    
    ContentText *content=[contentArray objectAtIndex:_selectedIndexPath.row];
    NSString *itemid=[NSString stringWithFormat:@"%d",content.itemid];
    [PointsManager delectCommentWithType:@"3" itemid:itemid completionBlock:^(BOOL isSuccess){
        
        if (isSuccess) {
            
            MBProgressHUD *hud = [Config createHUD];
            hud.mode = MBProgressHUDModeCustomView;
            hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"done"]];
            hud.labelText = @"删除评论成功!";
            [hud hide:YES afterDelay:1];
            
            [self getPingLunInfoWithPage:1];
            
        }else{
            
            MBProgressHUD *hud = [Config createHUD];
            hud.mode = MBProgressHUDModeCustomView;
            hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"error"]];
            hud.labelText = @"删除失败!";
            [hud hide:YES afterDelay:1];
            return ;
        }
        
    }];
}


@end
